package com.hj.dapp.config.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.PutObjectRequest;
import com.hj.dapp.common.constant.OssConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2024/1/2
 */
@Configuration
public class OssConfig {
    @Bean
    public OSS oss() {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = OssConstant.ENDPOINT;
        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        DefaultCredentialProvider credentialsProvider = null;
        try {
            credentialsProvider = CredentialsProviderFactory.newDefaultCredentialProvider(OssConstant.ACCESS_KEY_ID, OssConstant.SECRET_ACCESS_KEY);
            // 创建OSSClient实例。
            return new OSSClientBuilder().build(endpoint, credentialsProvider);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
